<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Production extends BaseModel
{
    use HasFactory;


    const CREATED_AT='create_time';
    const UPDATED_AT='change_time';

    protected $table = 'production';


    /**
     * 关联作者名
     */
    public function author(){
        return $this->belongsTo('App\Models\Authors' , 'author_id');
    }
     /**
     * 关联类型名
     */
    public function typeName(){
        return $this->belongsTo('App\Models\ProductionType' , 'type_id');
    }
     /**
     * 管理员名
     */
    public function manageInfo(){
        return $this->belongsTo('App\Models\Manage' , 'manage_id');
    }

    /**
     * 修改作品名称，加上书名号
     */
    public function getProNameAttribute($pro_name){
        if(strpos($pro_name , '《') === false || strpos($pro_name , '》') === false){
            return '《'.$pro_name.'》';
        }
        return $pro_name;
    }

    /**
     * 根据所有的作品id  获取所有的作品信息 (作者及类型信息)
     * @param production_ids
     */
     public function getProduction($production_ids){
        return $this->whereIn('id' , $production_ids)->with(['author'=>function($query){
            $query->select('id' , 'username' ,'username_en' , 'birthday','sex','intro','intro_en');
        },'typeName'=>function($query){
            $query->select('id' , 'name' ,'intro');
        }])->where('is_del' , 1)->get();
     }


     /**
     * 作品添加
     * @param $data 添加的数据
     */
    public function add($data){
        $this->pro_name = $data->pro_name;
        $this->pro_name_en = $data->pro_name_en;
        $this->texture  = $data->texture;
        $this->texture_en  = $data->texture_en;
        $this->year  = $data->year;
        $this->author_id = $data->author_id;
        $this->type_id  = $data->type_id;
        $this->intro  = $data->intro;
        $this->intro_en  = $data->intro_en;
        $this->img  = $data->img;
        $this->img_thumb = str_replace('.' , '_thumb.' , $data->img);
        $this->width  = $data->width;
        $this->height  = $data->height;
        $this->roll_speed = $data->roll_speed;
        $this->page_stay_min_time  = $data->page_stay_min_time;
        $this->template  = $data->template;
        $this->is_del = 1;
        $this->manage_id = $data->manage_id;

        return  $this->save();
    }

    /**
     *  作品修改
     * @param $data 添加的数据
     */
    public function change($data){
        $res = $this->where('is_del' , 1)->find($data['id']);
        if(!$res){
            return false;
        }

        $res->pro_name = $data->pro_name;
        $res->pro_name_en = $data->pro_name_en;
        $res->texture  = $data->texture;
        $res->texture_en  = $data->texture_en;
        $res->year  = $data->year;
        $res->author_id = $data->author_id;
        $res->type_id  = $data->type_id;
        $res->intro  = $data->intro;
        $res->intro_en  = $data->intro_en;
        $res->img  = $data->img;
        $res->img_thumb = str_replace('.' , '_thumb.' , $data->img);
        $res->width  = $data->width;
        $res->height  = $data->height;
        $res->roll_speed = $data->roll_speed;
        $res->page_stay_min_time  = $data->page_stay_min_time;
        $res->template  = $data->template;

        return  $res->save();
    }


    /**
     * 判断是否已经存在
     * @param pro_name 作品名称
     * @param id 作品id   可选，主要是用于修改
     */
    public function productionNameIsExists($name , $id = null){
        $res = $this->where('pro_name' , $name)->where(function($query) use($id){
                if(!empty($id)){
                    $query->where('id' , '<>' , $id);
                }
        })->where('is_del' , 1)->first();
        return $res;
    }


   



}
